package com.qire.manhua.model.viewModel.impl;

import android.view.View;
import android.widget.FrameLayout;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.AdvertManage;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.dal.WarehouseFactoryVo;
import com.qire.common.support.base.IViewModel;
import com.qire.common.utils.CountDownTimerBuilder;
import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.common.widget.ReferBinder;
import com.qire.manhua.BR;
import com.qire.manhua.dal.IAppConfigDAL;
import com.qire.manhua.model.entity.AppInternalAdvert;

import java.util.concurrent.TimeUnit;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

public class MainViewModel extends BaseViewModel<IViewModel> implements IViewModel {

    private final IAppConfigDAL appEnterAdDAL = WarehouseFactoryVo.create(this, IAppConfigDAL.class);

    public final ReferBinder<FrameLayout> advertContainerRef = new ReferBinder();

    public MainViewModel() {
        setSkipBtnVisible(View.GONE);
    }


    @Override
    public int getVariableId() {
        return BR.mainViewModel;
    }


    //region #业务与数据

    public void playAd() {
        appEnterAdDAL.fetchAdInfo();
    }

    /** 正常入场 */
    public void normalEnter() {
        updateCloseState(true);
    }

    /** 播放App站内广告 */
    public void playAppInternalAd(AppInternalAdvert advert) {
        countdownStart(advert.advertDuration);

        // todo 需要实现App站内广告图片插入的逻辑
    }

    /** 播放第三方广告 */
    public void playThirdPartyAD(int adTimeout) {
        countdownStart(adTimeout);

        FrameLayout advertContainer = advertContainerRef.get();
        if(ObjectUtil.nonNull(advertContainer)) {
            int advertWidth = EquipmentInfoUtil.getScreenWidth();
            int advertHeight = EquipmentInfoUtil.getScreenHeight() - EquipmentInfoUtil.dip2px(100);
            AdvertManage.AdvertSpace.AppSplash.build()
                    .setSize(advertWidth, advertHeight)
                    .setCompleteListener(() -> {

                    })
                    .setErrorListener(() -> {

                    })
                    .injectionSplash(advertContainer);
        }
    }

    //endregion #业务与数据 end

    /** 启动倒计时 */
    private void countdownStart(long countDownDuration) {
        CountDownTimerBuilder.build(countDownDuration,1, TimeUnit.SECONDS)
                .addTickCallBack(this::updateCountdown)
                .addFinishCallBack(this::finish)
                .start();
        setSkipBtnVisible(View.VISIBLE);
    }

    private long updateCountdown(long countDownTime, TimeUnit timeUnit) {
        setCountdownText("跳过 " + (countDownTime--) + "s");
        return countDownTime;
    }

    private Long finish(Long countDownTime, TimeUnit timeUnit) {
        updateCountdown(countDownTime, timeUnit);
        normalEnter();
        return countDownTime;
    }

    //region #UI操作
    private void setSkipBtnVisible(int visible) {
        put("skipBtnVisible", visible);
    }

    private void setCountdownText(CharSequence countdownText) {
        put("countdownText", countdownText);
    }

    /**
     * 更新关闭状态
     * @param state 将会广播通知的关闭状态，
     */
    public void updateCloseState(boolean state) {
        put("CloseState", state);
    }
    //endregion #UI操作 end

    //region #UI绑定
    public ReferBinder<FrameLayout> getAdvertContainerRef() {
        return advertContainerRef;
    }

    public MutableLiveData<Integer> getSkipBtnVisible() {
        return get("skipBtnVisible");
    }

    public MutableLiveData<CharSequence> getCountdownText() {
        return get("countdownText");
    }

    /**
     * 添加一个关闭状态的观察，用于通知页面关闭，如果状态为true则希望观察者们能调起页面关闭行为。
     * @param observer 观察者
     */
    public void addCloseStateObserver(Observer<Boolean> observer) {
        observeForever("CloseState", observer);
    }
    //endregion #UI绑定 end
}
